#include "DbStorage/RemoveRootRequirementStorageCmd.hpp"
#include "Model/Requirement.hpp"
#include "Model/Package.hpp"
#include "CaseDB/Connection.hpp"
#include "CaseDB/Statement.hpp"
#include <cassert>

namespace DbStorage
{


RemoveRootRequirementStorageCmd::RemoveRootRequirementStorageCmd( Model::Requirement& subject, Model::Package* pParentPackage, CaseDB::Connection& dbConnection )
	: DbStorageCmd( subject, Storage::StorageCommand::Delete, dbConnection)
	, pParentPackage_( pParentPackage)
{
	assert( !subject.hasParent());
	assert( NULL != pParentPackage_);
}

// ---------------------------------------------------------------------------------------------------------------------

void RemoveRootRequirementStorageCmd::execute()
{
	using namespace Model;
	using namespace CaseDB;

	Requirement& requirement = dynamic_cast< Requirement&>( rSubject_);
	assert( !requirement.storedID().empty());

	Statement deleteStatement( "{call DELETE_REQUIREMENT( ?, ?)}");
	deleteStatement.addParameter( DbTypesEnum::Integer, requirement.storedID());
	deleteStatement.addParameter( DbTypesEnum::Integer, pParentPackage_->storedID());

	dbConnection_.execute( deleteStatement);
}

} // namespace DbStorage
